package com.itchen.hospital.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.itchen.hospital.exception.CustomException;
import com.itchen.hospital.mapper.AppointmentMapper;
import com.itchen.hospital.mapper.DoctorMapper;
import com.itchen.hospital.mapper.PatientMapper;
import com.itchen.hospital.mapper.SchedulingMapper;
import com.itchen.hospital.pojo.*;
import com.itchen.hospital.service.AppointmentService;
import com.itchen.hospital.service.DoctorService;
import com.itchen.hospital.service.SchedulingService;
import com.itchen.hospital.utils.JwtTokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.TextStyle;
import java.util.Date;
import java.util.List;
import java.util.Locale;

@Service
public class AppointmentServiceImpl implements AppointmentService {

    @Autowired
    private DoctorMapper doctorMapper;
    @Autowired
    private AppointmentMapper appointmentMapper;
    @Autowired
    private SchedulingMapper schedulingMapper;
    @Autowired
    private PatientMapper patientMapper;

    //条件查询
    @Override
    public PageInfo<Doctor> search(Params params) {
        //添加一个星期几的查询条件
        String todayWeek = getTodayWeek();
        params.setWeek(todayWeek);
        //处理分页
        PageHelper.startPage(params.getPageNum(), params.getPageSize());
        List<Doctor> list = appointmentMapper.findBySearch(params);
        return PageInfo.of(list);
    }

    /**
     * 从系统时间获取到今天是星期几
     */
    private String getTodayWeek() {
//        switch (new java.util.Date().getDay()) {
//            case 1:
//                return "星期一";
//            case 2:
//                return "星期二";
//            case 3:
//                return "星期三";
//            case 4:
//                return "星期四";
//            case 5:
//                return "星期五";
//            case 6:
//                return "星期六";
//            case 7:
//                return "星期日";
//        }
        LocalDate now = LocalDate.now();
        DayOfWeek dayOfWeek = now.getDayOfWeek();
        return dayOfWeek.getDisplayName(TextStyle.FULL_STANDALONE, java.util.Locale.CHINA);
    }


    //挂号
    @Override
    @Transactional
    public void update(Params params) throws ParseException {
        Integer id = params.getId();
        //查询医生信息
        Doctor doctor = doctorMapper.findById(id);
        //再通过医生id查询排班信息
        String todayWeek = getTodayWeek();
        Scheduling scheduling = schedulingMapper.findByDoctorId(doctor.getId(), todayWeek);
        //判断是否还有号
        if (scheduling.getNum() == 0) {
            throw new CustomException("该医生今日已无号");
        }
        //从请求头中获取当前用户
        Admin currentUser = JwtTokenUtils.getCurrentUser();
        //将挂号信息存入挂号表
        Patient patient = new Patient();
        patient.setDoctorName(doctor.getUsername());
        patient.setPatientName(currentUser.getUsername());
        //将前端传来的预约时间，转换格式 前端传的格式为CTS
        if (params.getTime() == null){
            throw new CustomException("请选择预约时间");
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH);
        Date parse = dateFormat.parse(params.getTime().toString());
        String time = sdf.format(parse);
        patient.setAppointmentTime(time);
        //状态默认为待叫号
        patient.setStatus("-1");
        patientMapper.add(patient);
        //再修改就诊数量
        scheduling.setNum(scheduling.getNum() - 1);
        schedulingMapper.update(scheduling);

        // todo 再去校验医生今天是否有排班
        // todo 校验用户的钱是否足够挂号
    }
}
